171. Excel 表列序号
为保证权益,题目请参考 171. Excel 表列序号(From LeetCode).
解决方案1
CPP
C++
//
// Created by lenovo on 2020-09-29.
//
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
class Solution {
public:
int titleToNumber(string s) {
int ans = 0;
for (int i = 0; i < s.length(); i++) {
int num = s[i] - 'A' + 1;
ans = ans * 26 + num;
}
return ans;
}
};
int main() {
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Python
python
# 171. Excel表列序号
# https://leetcode-cn.com/problems/excel-sheet-column-number/
class Solution:
def titleToNumber(self, columnTitle: str) -> int:
ans = 0
for i, n in enumerate(reversed(columnTitle)):
ans += (ord(n) - ord("A") + 1) * (26 ** i)
return ans
if __name__ == "__main__":
solution = Solution()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15